local x = 1
local filename = "$orig/stock_across`x'.dta"
local lagamr = "lag`x'_amr"

use "`filename'", clear
egen idtime = group(eventdate)
drop if idtime<=`x'
by eventdate amr, sort: egen tot_workers = total(stock_across)
gen share = stock_across/tot_workers

preserve
keep if `lagamr'==.
collapse (mean) share, by(eventdate)
sum share
tsset eventdate
seasonallyadjust share, gen(share_sa) ar(1) ma(1) x11 additive replace
tsset eventdate
tsline share_sa
restore

drop if amr ==  `lagamr'
drop if `lagamr'==. 
by eventdate amr, sort: egen tot_flows = total(stock_across)
keep amr eventdate tot*
duplicates drop
sum eventdate
drop if eventdate==`r(min)'
gen inflow = tot_flows/tot_workers
drop tot*
save temp.dta, replace

use "`filename'", clear
by eventdate `lagamr', sort: egen tot_workers = total(stock_across)
keep if amr ==  `lagamr'
gen tot_flows = tot_workers - stock_across
keep amr eventdate tot*
duplicates drop
sum eventdate
drop if eventdate==`r(min)'
xtset amr eventdate
gen outflow = tot_flows/tot_workers
drop tot*
merge 1:1 amr eventdate using temp.dta
drop _merge
save temp.dta, replace

use temp.dta, clear
collapse (mean) inflow outflow, by(eventdate)
tsset eventdate
replace outflow = outflow*100
replace inflow = inflow*100
seasonallyadjust outflow, gen(outflow_sa) ar(1) ma(1) x11 additive replace
seasonallyadjust inflow, gen(inflow_sa) ar(1) ma(1) x11 additive replace
gen ma_outflow = (l.outflow_sa+outflow_sa+f.outflow_sa)/3
gen ma_inflow = (l.inflow_sa+inflow_sa+f.inflow_sa)/3
* this graph to show that if anything mobility looks procyclical
twoway (tsline ma_outflow, lcolor(black)) (lfit ma_outflow eventdate, lcolor(black) lpattern(dash)), graphregion(color(white)) xtitle("") ytitle("Average outflow") tline(1992q1 2001q1 2008q3, lcolor(red)) tline(1997q4 2005q2 2009q4, lcolor(green))
graph export "$graphs/outflow.png", replace
twoway (tsline ma_inflow, lcolor(black)) (lfit ma_inflow eventdate, lcolor(black) lpattern(dash)), graphregion(color(white)) xtitle("") ytitle("Average inflow") tline(1992q1 2001q1 2008q3, lcolor(red)) tline(1997q4 2005q2 2009q4, lcolor(green))
graph export "$graphs/inflow.png", replace
********************************************************************************
global version = 1
do ${do}/settingglobals.do
use localemployment${version}.dta, clear
keep eventdate employment_point* logemployment*
sort eventdate
gen idtime=_n
qui: reshape long employment_point logemployment logemployment_sa, i(eventdate) j(amr)
merge 1:1 amr eventdate using temp.dta
drop _merge
replace outflow = outflow*100
replace inflow = inflow*100
drop if idtime<=`x'
xtset amr eventdate
qui: gen peak = 0
qui: replace peak = 1 if employment_point == 1
qui: gen trough = 0 
qui: replace trough = 1 if employment_point == -1
xtset amr eventdate
gen change_employment = logemployment_sa-l.logemployment_sa
qui: tab eventdate, gen(date_)
local varlist = "peak trough"
foreach var of local varlist{
if "`var'"=="peak"{
local variable = "outflow"
}
else if "`var'"=="trough"{
local variable = "inflow"
}
/*
local var = "change_employment"
local varlist = "outflow inflow"
foreach variable of local varlist{
*/
global dep_var = "`variable'"
global indep_var = "`var'"

preserve
xtset amr eventdate

qui:xtreg ${dep_var} f4.${indep_var} date_*, fe vce(robust)
mat A=e(b)
mat B=e(V)
gen beta1 = A[1,1]
gen ub1 = A[1,1] + ${x}*B[1,1]^.5
gen lb1 = A[1,1] - ${x}*B[1,1]^.5

qui:xtreg ${dep_var} f3.${indep_var} date_*, fe vce(robust)
mat A=e(b)
mat B=e(V)
gen beta2 = A[1,1]
gen ub2 = A[1,1] + ${x}*B[1,1]^.5
gen lb2 = A[1,1] - ${x}*B[1,1]^.5

qui:xtreg ${dep_var} f2.${indep_var} date_*, fe vce(robust)
mat A=e(b)
mat B=e(V)
gen beta3 = A[1,1]
gen ub3 = A[1,1] + ${x}*B[1,1]^.5
gen lb3 = A[1,1] - ${x}*B[1,1]^.5

qui:xtreg ${dep_var} f1.${indep_var} date_*, fe vce(robust)
mat A=e(b)
mat B=e(V)
gen beta4 = A[1,1]
gen ub4 = A[1,1] + ${x}*B[1,1]^.5
gen lb4 = A[1,1] - ${x}*B[1,1]^.5

qui:xtreg ${dep_var} ${indep_var} date_*, fe vce(robust)
mat A=e(b)
mat B=e(V)
gen beta5 = A[1,1]
gen ub5 = A[1,1] + ${x}*B[1,1]^.5
gen lb5 = A[1,1] - ${x}*B[1,1]^.5

qui:xtreg ${dep_var} l1.${indep_var} date_*, fe vce(robust)
mat A=e(b)
mat B=e(V)
gen beta6 = A[1,1]
gen ub6 = A[1,1] + ${x}*B[1,1]^.5
gen lb6 = A[1,1] - ${x}*B[1,1]^.5

qui:xtreg ${dep_var} l2.${indep_var} date_*, fe vce(robust)
mat A=e(b)
mat B=e(V)
gen beta7 = A[1,1]
gen ub7 = A[1,1] + ${x}*B[1,1]^.5
gen lb7 = A[1,1] - ${x}*B[1,1]^.5

qui:xtreg ${dep_var} l3.${indep_var} date_*, fe vce(robust)
mat A=e(b)
mat B=e(V)
gen beta8 = A[1,1]
gen ub8 = A[1,1] + ${x}*B[1,1]^.5
gen lb8 = A[1,1] - ${x}*B[1,1]^.5

qui:xtreg ${dep_var} l4.${indep_var} date_*, fe vce(robust)
mat A=e(b)
mat B=e(V)
gen beta9 = A[1,1]
gen ub9 = A[1,1] + ${x}*B[1,1]^.5
gen lb9 = A[1,1] - ${x}*B[1,1]^.5

keep beta* ub* lb*
duplicates drop
gen aux = 1
reshape long beta ub lb, i(aux) j(lag)
qui: label define lag_lbl 1 "4Q before" 2 "3Q before" 3 "2Q before" 4 "1Q before" 5 "`var'" 6 "1Q after" 7 "2Q after" 8 "3Q after" 9 "4Q after"
qui: label values lag lag_lbl
tsset lag
gen horiz=0
twoway (line horiz lag, lcolor(red)) (sc beta lag, connect(direct) lcolor(black) mcolor(black)) (rcap ub lb lag, lcolor(black)), legend(off) graphregion(color(white)) xlabel(1(1)9, valuelabel angle(45)) xtitle("")
graph export "$graphs/mobility`variable'.png", replace
restore
}

********************************************************************************
/*global version = 1
do ${do}/settingglobals.do
use qoq_concentration_72_sa_${version}.dta, clear
keep eventdate idtime amr cycle_linear* 
sort eventdate
merge 1:1 amr eventdate using temp.dta
drop _merge
drop if idtime<3
gen d=cycle_linear-l.cycle_linear
local var "cycle_linear"
qui: tab eventdate, gen(date_)
local varlist = "outflow inflow"
foreach variable of local varlist{
preserve
qui: xtreg `variable' l1.`var' date_*, fe vce(cluster amr)
qui: return list 
mat A = e(b)
gen corr1= A[1,1]
mat B=e(V)
gen ub1 = A[1,1] + ${x}*B[1,1]^.5
gen lb1 = A[1,1] - ${x}*B[1,1]^.5
qui: xtreg `variable' l2.`var' date_*, fe vce(cluster amr)
qui: return list 
mat A = e(b)
mat B=e(V)
gen corr2= A[1,1]
gen ub2 = A[1,1] + ${x}*B[1,1]^.5
gen lb2 = A[1,1] - ${x}*B[1,1]^.5
qui: xtreg `variable' l3.`var' date_*, fe vce(cluster amr)
qui: return list 
mat A = e(b)
mat B=e(V)
gen corr3= A[1,1]
gen ub3 = A[1,1] + ${x}*B[1,1]^.5
gen lb3 = A[1,1] - ${x}*B[1,1]^.5
qui: xtreg `variable' l4.`var' date_*, fe vce(cluster amr)
qui: return list 
mat A = e(b)
mat B=e(V)
gen corr4= A[1,1]
gen ub4 = A[1,1] + ${x}*B[1,1]^.5
gen lb4 = A[1,1] - ${x}*B[1,1]^.5
keep corr* ub* lb*
duplicates drop
gen aux = 1
reshape long corr ub lb, i(aux) j(lag)
tsset lag
twoway (tsline corr) (tsline ub, lpattern(dash)) (tsline lb, lpattern(dash))
graph export "$graphs/mobility_`var'.png", replace
restore
}*/








